﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PrefabsMgrInitConfigBase
{
    public int BulletCacheCount = 0;
    public int BulletCount = 0;
    public int BulletBombCount = 0;
    public int BombCount = 0;
    public int IdiotCount = 0;

    public int HWSCount = 0;
    public int HMSCount = 0;
    public int GFCount = 0;



    public Transform BattleGround = null;
    public PrefabsMgrInitConfigBase(
        int bulletCacheCount,
        int bulletCount,
        int bulletBombCount,
        int bombCount,
        int idiotCount,
        int hwsCount,
        int hmsCount,
        int gfCount,
        Transform battleGround)
    {

        this.HMSCount = hmsCount;
        this.HWSCount = hwsCount;
        this.GFCount = gfCount;

        this.BulletCacheCount = bulletCacheCount;
        this.BulletCount = bulletCount;
        this.BulletBombCount = bulletBombCount;
        this.BombCount = bombCount;
        this.IdiotCount = idiotCount;
        this.BattleGround = battleGround;

    }
}


public enum EnumItemState { 
    E_ACTIVE,
    E_IDLE,
    E_SLEEP,
}

public class PrefabsMgr : MonoBehaviour
{
    private static PrefabsMgr instance_ = null;
    public static PrefabsMgr GetInstance()
    {
        if (instance_ == null)
        {
            Debug.LogError("PrefabsMgr instance_ = null");
        }
        return instance_;
    }


    // Start is called before the first frame update
    private void Awake()
    {
        instance_ = this;
    }

    // ---- 节点 -----
    public Transform TActors = null;
    public Transform TEffects = null; 


    // ---- 预制列表 -----
    // -- 火力类型
    public GameObject PfBulletCache = null;
    public GameObject PfBulletCommon = null;
    public GameObject PfBulletBomb = null;
    public GameObject PfBomb = null;

    public GameObject PfHitWallSpark = null;
    public GameObject PfHitManSpark = null;
    public GameObject PfGunFire = null;

    // -- 怪物类型
    public GameObject PfIdiot = null;

    // -- 尸体
    public GameObject PfIdiotDeadBody = null;


    // ----- 预制实例缓存池 -----
    public static string TYPE_BULLET_CACHE = "TYPE_BULLET_CACHE";
    public static string TYPE_BULLET = "TYPE_BULLET";
    public static string TYPE_BULLET_BOMB = "TYPE_BULLET_BOMB";
    public static string TYPE_BOMB = "TYPE_BOMB";
    public static string TYPE_IDIOT = "TYPE_IDIOT";

    public static string TYPE_HITWALLSPARK = "TYPE_HITWALLSPARK";
    public static string TYPE_HITMANSPARK = "TYPE_HITMANSPARK";
    public static string TYPE_GUNFIRE = "TYPE_GUNFIRE";

    public static string TYPE_ENEMY_BODY = "TYPE_ENEMY_BODY";

    protected Dictionary<string, List<PrefabCommonBase>> dicOfCaches_ = new Dictionary<string, List<PrefabCommonBase>>();

    protected Transform transformOfBattleGround_ = null;

    protected void initPfCache(
        int count,
        string type,
        GameObject pf,
        Dictionary<string, List<PrefabCommonBase>> dicOfCache,Transform parent)
    {

        List<PrefabCommonBase> list = new List<PrefabCommonBase>();
        for (var i = 0; i < count; ++i)
        {
            var g = GameObject.Instantiate(pf) as GameObject;
            g.transform.SetParent(parent);
            var item = g.GetComponent<PrefabCommonBase>();
            list.Add(item);
            item.State = EnumItemState.E_SLEEP;
        }
        dicOfCache.Add(type, list);
    }

    protected PrefabCommonBase tryGetItemByState(List<PrefabCommonBase> list, EnumItemState state)
    {
        var count = list.Count;
        for (var i = 0; i < count; ++i)
        {
            var item = list[i];
            if (item.State == state)
            {
                return item;
            }
        }
        return null;
    }


    // ----- 对外接口 -----
    public void Init(PrefabsMgrInitConfigBase config)
    {
        this.transformOfBattleGround_ = config.BattleGround;

        /*
         *         this.HMSCount = hmsCount;
        this.HWSCount = hwsCount;
        this.GFCount = gfCount;

         * */



        this.initPfCache(
             50,
             PrefabsMgr.TYPE_ENEMY_BODY,
             PfIdiotDeadBody,
             this.dicOfCaches_, this.TEffects);


        this.initPfCache(
            config.HMSCount,
            PrefabsMgr.TYPE_HITMANSPARK,
            PfHitManSpark,
            this.dicOfCaches_ ,this.TEffects);
        this.initPfCache(
            config.HWSCount,
            PrefabsMgr.TYPE_HITWALLSPARK,
            PfHitWallSpark,
            this.dicOfCaches_, this.TEffects);
         this.initPfCache(
            config.GFCount,
            PrefabsMgr.TYPE_GUNFIRE,
            PfGunFire,
            this.dicOfCaches_, this.TEffects);


        this.initPfCache(
            config.BulletCacheCount,
            PrefabsMgr.TYPE_BULLET_CACHE,
            PfBulletCache,
            this.dicOfCaches_, this.TEffects);
        this.initPfCache(
            config.BulletCount,
            PrefabsMgr.TYPE_BULLET,
            PfBulletCommon,
            dicOfCaches_, this.TEffects);
        this.initPfCache(
            config.BulletBombCount,
            PrefabsMgr.TYPE_BULLET_BOMB,
            PfBulletBomb,
            dicOfCaches_, this.TEffects);
        this.initPfCache(
            config.BombCount,
            PrefabsMgr.TYPE_BOMB,
            PfBomb,
            dicOfCaches_, this.TEffects);
        this.initPfCache(
            config.IdiotCount,
            PrefabsMgr.TYPE_IDIOT,
            PfIdiot,
            dicOfCaches_, this.TActors);

    }


    public void Send(string pfType, WakeUpConfigBase config = null) {
        var list = this.dicOfCaches_[pfType];
        if (list != null) {
            var item = this.tryGetItemByState(list,EnumItemState.E_SLEEP);
            if (item != null) {
                item.State = EnumItemState.E_ACTIVE;
                item.SetStatusByConfig(config);
            }
        }
    }

    public void RecycleType(string type) {
        var list = this.dicOfCaches_[type];
        if (list != null)
        {
            var count = list.Count;
            for (var i = 0;i<count;++i) {
                list[i].State = EnumItemState.E_SLEEP;
            }
        }
    }
}
